//
//  PlayMulitplePopView.m
//  GC_OC
//
//  Created by wlf on 2022/5/15.
//

#import "PlayMulitplePopView.h"
#import "MulitpleTableViewCell.h"
@implementation PlayMulitplePopView
- (void)showIn:(UIView *)view currentMulitple:(nonnull NSString *)mulitStr
{
    [view addSubview:self];
    self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.1];
    
    for (NSDictionary *dict in self.dataSource) {
        NSString *titleStr = dict[@"title"];
        if (titleStr.floatValue == mulitStr.floatValue) {
            self.index = [self.dataSource indexOfObject:dict];
            [self.mulitTabView selectRowAtIndexPath:[NSIndexPath indexPathForRow:self.index inSection:0] animated:YES scrollPosition:UITableViewScrollPositionNone];
            break;
        }
        
    }
    
}
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeViewWithTap)];
        tap.delegate = self;
        [self addGestureRecognizer:tap];
        UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 458, SCREEN_WIDTH, 458)];
        self.contentView = contentView;
        contentView.backgroundColor = [UIColor whiteColor];
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, SCREEN_WIDTH, 1000) byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(12, 12)];
         CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
         maskLayer.frame = contentView.bounds;
         maskLayer.path = maskPath.CGPath;
        contentView.layer.mask = maskLayer;
        [self addSubview:contentView];
    
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(31, 14, 80, 22)];
        titleLabel.text = @"倍数设定";
        titleLabel.font = [UIFont boldSystemFontOfSize: 16];
        titleLabel.textColor = [UIColor blackColor];
        [contentView addSubview:titleLabel];
        
        UILabel *subTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(110, 19, SCREEN_WIDTH - 110, 17)];
        subTitleLabel.text = @"（根据语速，调整速度，正常交流为 1.0倍）";
        subTitleLabel.font = [UIFont systemFontOfSize: 12];
        subTitleLabel.textColor = [UIColor blackColor];
        [contentView addSubview:subTitleLabel];
        
        self.mulitTabView.frame = CGRectMake(0, 36, SCREEN_WIDTH, 458 - 36);
        [contentView addSubview:self.mulitTabView];
        self.dataSource = @[
        @{
            @"title": @"0.75",
            @"subTitle": @"微慢"
        },
        @{
            @"title": @"1.0",
            @"subTitle": @"正常"
        },
        @{
            @"title": @"1.25",
            @"subTitle": @"微快"
        },
        @{
            @"title": @"1.5",
            @"subTitle": @"偏快"
        },
        @{
            @"title": @"2.0",
            @"subTitle": @"很快"
        },
        @{
            @"title": @"2.5",
            @"subTitle": @"超快"
        },
        @{
            @"title": @"3.0",
            @"subTitle": @"最快"
        }];
        
        [self.mulitTabView reloadData];
    }
    return self;
}
- (void)closeViewWithTap{
    
    [UIView animateWithDuration:0.3 animations:^{
           
           self.contentView.transform = CGAffineTransformMakeTranslation(0, 458);
       } completion:^(BOOL finished) {
           
           [self removeFromSuperview];
           [self.contentView removeFromSuperview];
           
       }];
}
#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    CGPoint point = [touch locationInView:self];
    if (point.y <= CGRectGetMinY(self.contentView.frame)) {
        
        return YES;
    }else{
        return NO;
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MulitpleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Mulitple" forIndexPath:indexPath];
    NSDictionary *dict = self.dataSource[indexPath.row];
    cell.titleLab.text = dict[@"title"];
    cell.subTitleLab.text = dict[@"subTitle"];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSDictionary *dict = self.dataSource[indexPath.row];
    NSString *mulit = dict[@"title"];
    if (self.mulitBlock) {
        self.mulitBlock(mulit);
        [self closeViewWithTap];
    }
}
- (UITableView *)mulitTabView{
    if (!_mulitTabView) {
        _mulitTabView = [[UITableView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 458, SCREEN_WIDTH, 458) style:UITableViewStylePlain];
        _mulitTabView.dataSource = self;
        _mulitTabView.delegate = self;
        _mulitTabView.showsVerticalScrollIndicator = NO;
        [_mulitTabView registerClass:[MulitpleTableViewCell class] forCellReuseIdentifier:@"Mulitple"];
        _mulitTabView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _mulitTabView;
}
@end
